/*    This file is part of rls.
 *
 *    Rls is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    Rls is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with rls.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef RLSR_H
#define RLSR_H

/*
Include:
something with size_t
#include <rid.h>
#include <stdint.h>

#include <unfy/unfy.h>

#include <rls/rls.h>

#include <rls/rlsr.h>
*/

#ifndef STYLE_9
#include <rls/rls.h>
#endif

typedef enum { RLSR_ASSERT, RLSR_SEARCH, RLSR_VIEW, RLSR_ERROR } Rlsr_cmd;

typedef struct {
	/* used by both assert and search */
	Rlsr_cmd cmd;
	Unfy_term *term;
	size_t term_pos;
	/* used by assert */
	Rid id;
	size_t id_pos;
	Unfy_list *list;
	size_t list_pos;
	/* used by search */
	int limit;
	size_t limit_pos;
} Rlsr_stmnt;

typedef struct Rlsr_lsnr Rlsr_lsnr;

typedef Unfy_stat (*Rlsr_lsn)(Rlsr_lsnr *lsnr, Rls_rule **rules,
			      Rls_srch **srch, char **str, size_t *size,
			      Rls_info *info, void *extra, Rls_recycle *rec);

typedef int (*Rlsr_assert)(Rlsr_lsnr *lsnr, Rlsr_stmnt *stmnt,
			   Rls_rule **rules, void *extra, Rls_recycle *rec);

typedef Unfy_stat (*Rlsr_search)(Rlsr_lsnr *lsnr, Rlsr_stmnt *stmnt,
				 Rls_srch **srch_ref, Rls_rule **rules,
				 Rls_info *info, void *extra,
				 Rls_recycle *rec);

typedef void (*Rlsr_error)(Rlsr_lsnr *lsnr, void *extra);

struct Rlsr_lsnr {
	Rlsr_lsn lsn;
	Rlsr_assert assert;
	Rlsr_search search;
	Rlsr_error error;
	Rls_bend bend;
	const char *error_str;
	size_t pos;
};

Rlsr_cmd
rlsr_cmd_parse(const char *str, size_t *pos, const char **error);

int
rlsr_stmnt_parse(Rlsr_stmnt *stmnt, const char *str, size_t *pos,
		 const char **error, Rls_recycle *rec);

Unfy_stat
rlsr_searching(Rlsr_lsnr *lsnr, Rlsr_stmnt *stmnt, Rls_rule **rules,
	       Rls_srch **srch, char **str, size_t *n, Rls_info *info,
	       void *extra, Rls_recycle *rec);

Unfy_stat
rlsr_asserting(Rlsr_lsnr *lsnr, Rlsr_stmnt *stmnt, Rls_rule **rules,
	       void *extra, Rls_recycle *rec);

void
rlsr_lsnr_init(Rlsr_lsnr *lsnr, Rlsr_lsn lsn, Rlsr_assert assert,
	       Rlsr_search search, Rlsr_error error, Rls_bend bend,
	       const char *error_str, size_t pos);

void
rlsr_repl(Rls_rule **rules, Rlsr_lsnr *lsnr, void *extra, Rls_recycle *rec);

Unfy_stat
rlsr_default_lsn(Rlsr_lsnr *lsnr, Rls_rule **rules, Rls_srch **srch,
		 char **str, size_t *size, Rls_info *info, void *extra,
		 Rls_recycle *rec);

int
rlsr_default_assert(Rlsr_lsnr *lsnr, Rlsr_stmnt *stmnt,
		    Rls_rule **rules, void *extra, Rls_recycle *rec);

Unfy_stat
rlsr_default_search(Rlsr_lsnr *lsnr, Rlsr_stmnt *stmnt, Rls_srch **srch_ref,
		    Rls_rule **rules, Rls_info *info, void *extra,
		    Rls_recycle *rec);

void
rlsr_default_error(Rlsr_lsnr *lsnr, void *extra);

#endif
